package com.cit.draw.redis;

import java.util.Map;
import java.util.Properties;

import com.google.common.collect.Maps;
import com.google.inject.AbstractModule;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import com.google.inject.TypeLiteral;
import com.google.inject.name.Names;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;

/**
 * This module enables caching by exposing
 * 
 * @author Vincent Zou
 * 
 */
public class CachingModule extends AbstractModule {

	@Override
	protected void configure() {
		// bind all MCMResource into a singleton provider so that every time I
		// would get the same resource

		bind(CachingProvider.class).to(
				RedisCachingProvider.class);

		// Redis Persisted Cache
		bind(PersistedCacheRequestHandler.class).to(
				PersistedCacheRequestHandlerImpl.class);


		// Some basic generic caching settings
		// TODO cache timeout should retrieve from the cluster and define a
		// default timeout locally.
		bind(new TypeLiteral<Map<String, Integer>>() {
		}).annotatedWith(Names.named("cacheTimeout"))
				.toProvider(new Provider<Map<String, Integer>>() {
					@Override
					public Map<String, Integer> get() {
						Map<String, Integer> testTimeouts = Maps.newHashMap();
						// here goes the default caching timeout table.
						testTimeouts
								.put("123",
										1234);
						return testTimeouts;
					}
				}).in(Singleton.class);

		// TODO local configurations should not happen when using cluster.
		final Config config = ConfigFactory.load();
		Properties props = new Properties();
//		String timeout = config
//				.getString("data-access-layer.cache.elements-ttl.default");
//		props.put("timeout", timeout);

		Names.bindProperties(binder(), props);

	}

}
